﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;
using TracNghiem.Areas.Admin.Models;
namespace TracNghiem.Models
{
    public class General
    {
        public static OracleConnection ConnectDataBase()
        {
            OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["tracnghiem"].ConnectionString);
            return conn;
        }
        public static OracleConnection conn_global;

        public static DataSet Lib_GetDataSet(string sql)
        {
            conn_global = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn_global);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn_global.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            return ds;
        }
        public static void Lib_ExecuteNonQuery(string sql)
        {
            conn_global = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn_global);
            try
            {
                conn_global.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            omd.ExecuteNonQuery();
            conn_global.Close();
        }
        
        public static OracleDataReader Lib_GetDataReader(string sql)
        {
            conn_global = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn_global);
            try
            {
                conn_global.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            OracleDataReader odr = omd.ExecuteReader();
            return odr;
        }
        public static int Lib_CheckExist(string sql)
        {
            conn_global = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn_global);
            try
            {
                conn_global.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            OracleDataReader odr = omd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(odr);
            conn_global.Close();
            return dt.Rows.Count;
        }

        public static string Get_select(string id=null,string tonghopdv=null)
        {
            DichvuModel dv = new DichvuModel();
            List<DichvuModel> dichvuModel = dv.getDichvu_byid(0,0);
            string kq = "";
            kq += "<option value=\"\">--Chọn dịch vụ--</option>";
            foreach (var item in dichvuModel)
            {
                if(id==item.id)
                    kq += "<option value=\"" + item.id + "\" selected='selected'>" + item.name + "</option>";
                else
                    kq += "<option value=\"" + item.id + "\">" + item.name + "</option>";
                List<DichvuModel> dvc = dv.getDichvu_byid(int.Parse(item.id),0);
                foreach (var con in dvc)
                {
                    if(id==con.id)
                        kq += "<option value=\"" + con.id + "\" selected='selected'>" + item.name + "-->" + con.name + "</option>";
                    else
                        kq += "<option value=\"" + con.id + "\">" + item.name + "-->" + con.name + "</option>";
                }
            }
            if(tonghopdv!=null)
                if(id=="-1")
                    kq += "<option value=\"-1\" selected='selected'>" + tonghopdv + "</option>";                    
                else
                    kq += "<option value=\"-1\" >" + tonghopdv + "</option>";
            return kq;

        }
    }
}